from django.shortcuts import render
from django.shortcuts import redirect
from django.shortcuts import HttpResponse

from blog.models import Author, Blog


# Create your views here.


def index(request):
    return redirect("blog:blogs")


def blogs(request):
    blogs = Blog.objects.all()

    return render(request, 'blogs.html', {"blogs": blogs})


def blog_add(request):
    if request.method == "GET":
        # 将作者信息查出来
        authors = Author.objects.all()
        return render(request, "blog_add.html", {"authors": authors})
    if request.method == "POST":
        title = request.POST.get("title")
        content = request.POST.get("content")
        author = request.POST.get("author")

        Blog.objects.create(title=title, content=content, author_id=author)
        return redirect("blog:index")


def blog_detail(request):
    blog_id = request.GET.get('id')
    blog = Blog.objects.get(id=blog_id)
    return render(request, 'blog.html', {"blog": blog})


def authors(request):
    authors = Author.objects.all()
    return render(request, 'authors.html', {"authors": authors})


def author_detail(request):
    author_id = request.GET.get("id")
    author = Author.objects.get(id=author_id)

    # print(author.blog_set.all())
    # print(author.blog_set())
    # blogs = Blog.objects.filter(author_id=author_id).all()  # 当前作者写过哪些作品
    # print(blogs)
    # blogs = author.blog_set.all() # 反向查询
    # return render(request, 'author_detail.html', {"author": author, "blogs": blogs})
    return render(request, 'author_detail.html', {"author": author})


def update_blog(request):
    if request.method == "GET":
        blog_id = request.GET.get('id')
        blog = Blog.objects.get(id=blog_id)
        authors = Author.objects.all()

        return render(request, 'update_blog.html', {"blog": blog, "authors": authors})
    if request.method == "POST":
        blog_id = request.POST.get("id")
        title = request.POST.get("title")
        content = request.POST.get("content")
        author = request.POST.get("author")
        print(author)

        # filter 的update不会触发 auto_now
        # Blog.objects.filter(id=blog_id).update(title=title, content=content, author_id=author)
        blog = Blog.objects.get(id=blog_id)
        blog.title = title
        blog.content = content
        blog.author_id = author
        blog.save()

        return redirect("blog:index")


from utils import fileutils


def author_add(request):
    if request.method == "GET":
        return render(request, 'author_add.html')
    if request.method == "POST":
        name = request.POST.get("name", "")
        birthday = request.POST.get("birthday", "")
        icon = request.FILES.get("icon", "")
        filepath = fileutils.saveupfilemd5(icon)
        introduce = request.POST.get("introduce", "")
        phone = request.POST.get("phone", "")
        email = request.POST.get("email", "")

        Author.objects.create(name=name, birthday=birthday,
                              icon=filepath,
                              introduce=introduce,
                              phone=phone,
                              email=email
                              )
        return redirect("blog:authors")


from django.db.models import Q


def fun1(request):
    print(request.path)
    # 查询条件有逻辑关系了
    # blog = Blog.objects.get(Q(id=5)|Q(title="红楼梦"))
    # blog = Blog.objects.get(Q(id=3) & Q(title="红楼梦"))
    # return HttpResponse(blog.author)
    print('--fun1--------------', id(request))
    # raise Exception("异常了")
    # Blog.objects.get(id=100)
    return HttpResponse('fun1')
